﻿Public Class frmSurvey


    ' ***************************************************************
    ' ********************** Private Functions **********************
    ' ***************************************************************

    Private Function ChangeAllInputBoxesStatus(ByVal NewStatus As Boolean) As Boolean

        SurveyIDTextBox.Enabled = NewStatus
        CustomerNameTextBox.Enabled = NewStatus
        CustomerPhoneMaskedTextBox.Enabled = NewStatus
        AgeComboBox.Enabled = NewStatus
        TodayDateTextBox.Enabled = NewStatus
        EmployeeIDComboBox.Enabled = NewStatus
        PromotionIDComboBox.Enabled = NewStatus
        TownComboBox.Enabled = NewStatus
        GenderComboBox.Enabled = NewStatus

    End Function

    Private Function GetReadyToAddNewItem() As Boolean
        ' This function will give a new Employee ID Randomly.
        ' Then it get ready to start filling in data. It will
        ' also disable the button so the user cannot try to
        ' save anything without entering the information first.

        Try
            Me.SurveyBindingSource.AddNew()
            ' Fill current date and time
            Me.TodayDateTextBox.Text = Date.Now
            ' Generate a new Survey ID
            Me.SurveyIDTextBox.Text = GenerateRandomNumber(100, 9999999).ToString
            ' Disables the add survey button to make sure the user add values
            btnAddSurvey.Enabled = False
            Return True
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Return False
        End Try
    End Function

    Private Function AddNewItem() As Boolean
        Try
            Me.Validate()
            Me.Atlantic_toyota_atm_dbDataSet.BeginInit()
            Me.SurveyBindingSource.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.Atlantic_toyota_atm_dbDataSet)
            Me.Atlantic_toyota_atm_dbDataSet.EndInit()
            Me.Atlantic_toyota_atm_dbDataSet.AcceptChanges()
            MessageBox.Show("Changes Saved.", "Success", MessageBoxButtons.OK, _
                            MessageBoxIcon.Information)
            Return True
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Return False
        End Try
    End Function

    Private Function ValidateSurvey() As Boolean
        Try
            If btnAddSurvey.Text.Length = 0 Then
                btnAddSurvey.Enabled = False
            Else
                btnAddSurvey.Enabled = True
            End If
            If SurveyIDTextBox.Text.Length = 0 Then
                btnAddSurvey.Enabled = False
            Else
                btnAddSurvey.Enabled = True
            End If
            If CustomerNameTextBox.Text.Length = 0 Then
                btnAddSurvey.Enabled = False
            Else
                btnAddSurvey.Enabled = True
            End If
            If CustomerPhoneMaskedTextBox.Text.Length = 0 Then
                btnAddSurvey.Enabled = False
            Else
                btnAddSurvey.Enabled = True
            End If
            If AgeComboBox.Text.Length = 0 Then
                btnAddSurvey.Enabled = False
            Else
                btnAddSurvey.Enabled = True
            End If
            If TodayDateTextBox.Text.Length = 0 Then
                btnAddSurvey.Enabled = False
            Else
                btnAddSurvey.Enabled = True
            End If
            If EmployeeIDComboBox.Text.Length = 0 Then
                btnAddSurvey.Enabled = False
            Else
                btnAddSurvey.Enabled = True
            End If
            If PromotionIDComboBox.Text.Length = 0 Then
                btnAddSurvey.Enabled = False
            Else
                btnAddSurvey.Enabled = True
            End If
            If TownComboBox.Text.Length = 0 Then
                btnAddSurvey.Enabled = False
            Else
                btnAddSurvey.Enabled = True
            End If
            If GenderComboBox.Text.Length = 0 Then
                btnAddSurvey.Enabled = False
            Else
                btnAddSurvey.Enabled = True
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
            Return False
        End Try
        Return True
    End Function

    ' ***************************************************************
    ' ************************ Private  Subs ************************
    ' ***************************************************************


    ' Load Form and get ready

    Private Sub frmSurvey_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            SurveyIDTextBox.ReadOnly = True
            TodayDateTextBox.ReadOnly = True

            ' Hides all the main menu labels

            lblHome.Visible = False
            lblVisit.Visible = False
            'lblSurvey.Visible = False
            lblStatistics.Visible = False
            lblInventory.Visible = False
            lblEmployee.Visible = False
            lblClose.Visible = False

            ' Fill data for the employee segment 
            Me.EmployeeTableAdapter.Fill(Me.Atlantic_toyota_atm_dbDataSet.employee)

            lblSave.Text = "Add New"
            btnAddSurvey.Image = GeneralImageSet.Images("Add")

            btnAddSurvey.Enabled = True
            ChangeAllInputBoxesStatus(False)

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub


    ' Just making sure Everything is filled up before we save
    ' anything

    Private Sub SurveyIDTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SurveyIDTextBox.TextChanged
        ValidateSurvey()
    End Sub

    Private Sub CustomerNameTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CustomerNameTextBox.TextChanged
        ValidateSurvey()
    End Sub

    Private Sub CustomerPhoneMaskedTextBox_MaskInputRejected(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MaskInputRejectedEventArgs) Handles CustomerPhoneMaskedTextBox.MaskInputRejected
        ValidateSurvey()
    End Sub

    Private Sub AgeComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AgeComboBox.SelectedIndexChanged
        ValidateSurvey()
    End Sub

    Private Sub TodayDateTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TodayDateTextBox.TextChanged
        ValidateSurvey()
    End Sub

    Private Sub EmployeeIDComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EmployeeIDComboBox.SelectedIndexChanged
        ValidateSurvey()
    End Sub

    Private Sub PromotionIDComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PromotionIDComboBox.SelectedIndexChanged
        ValidateSurvey()
    End Sub

    Private Sub TownComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TownComboBox.SelectedIndexChanged
        ValidateSurvey()
    End Sub

    Private Sub GenderComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GenderComboBox.SelectedIndexChanged
        ValidateSurvey()
    End Sub

    ' Lets add the data

    Private Sub btnAddSurvey_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddSurvey.Click
        Try

            If lblSave.Text = "Add New" Then

                ' If I am here is because I just came to the form
                ' or because I just saved a value and is starting
                ' up again. So we will enable the text boxes and 
                ' change the button label to Save to make it 
                ' consistent.
                btnAddSurvey.Image = GeneralImageSet.Images("Save")
                lblSave.Text = "Save"
                ChangeAllInputBoxesStatus(True)
                GetReadyToAddNewItem()
                CustomerNameTextBox.Focus()

            ElseIf lblSave.Text = "Save" Then

                ' If I am here is because Im about to add a value 
                ' So we will disable the text boxes and change 
                ' the button label to Add New to make it consistent.

                btnAddSurvey.Image = GeneralImageSet.Images("Add")
                lblSave.Text = "Add New"
                ChangeAllInputBoxesStatus(False)
                AddNewItem()
                lblSurvey.Focus()

            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    ' Lets close it all

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        CloseAllForms()
    End Sub

    Private Sub btnClose_Visit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Dim Confirmation As MsgBoxResult
        Confirmation = MessageBox.Show("Unsaved changes will be lost. Are you sure you want to exit?", _
                                       "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If Confirmation = MsgBoxResult.Yes Then
            CloseAllForms()
        End If
    End Sub

    Private Sub btnHome_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHome.Click
        SwitchForm(My.Forms.frmMain)
    End Sub

    Private Sub btnVisit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVisit.Click
        SwitchForm(My.Forms.frmQuickVisit)
    End Sub

    Private Sub btnStatistics_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStatistics.Click
        SwitchForm(My.Forms.frmStatistics)
    End Sub

    Private Sub btnInventory_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInventory.Click
        SwitchForm(My.Forms.frmViewInventory)
    End Sub

    Private Sub btnEmployee_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEmployee.Click
        SwitchForm(My.Forms.frmViewEmployee)
    End Sub


    '************************************************
    ' Show the label for the items on the main menu *
    '************************************************

    Private Sub btnHome_MouseOver(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnHome.MouseHover
        lblHome.Visible = True
    End Sub

    Private Sub btnVisit_MouseOver(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnVisit.MouseHover
        lblVisit.Visible = True
    End Sub

    Private Sub btnStatistics_MouseOver(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnStatistics.MouseHover
        lblStatistics.Visible = True
    End Sub

    Private Sub btnInventory_MouseOver(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnInventory.MouseHover
        lblInventory.Visible = True
    End Sub

    Private Sub btnEmployee_MouseOver(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnEmployee.MouseHover
        lblEmployee.Visible = True
    End Sub

    Private Sub btnClose_MouseOver(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnClose.MouseHover
        lblClose.Visible = True
    End Sub


    '**************************************
    ' Hides the labels from the main menu *
    '**************************************


    Private Sub btnHome_MouseLeave(ByVal sender As System.Object, _
              ByVal e As System.EventArgs) Handles btnHome.MouseLeave
        lblHome.Visible = False
    End Sub

    Private Sub btnVisit_MouseLeave(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnVisit.MouseLeave
        lblVisit.Visible = False
    End Sub

    Private Sub btnStatistics_MouseLeave(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnStatistics.MouseLeave
        lblStatistics.Visible = False
    End Sub

    Private Sub btnInventory_MouseLeave(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnInventory.MouseLeave
        lblInventory.Visible = False
    End Sub

    Private Sub btnEmployee_MouseLeave(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnEmployee.MouseLeave
        lblEmployee.Visible = False
    End Sub

    Private Sub btnClose_MouseLeave(ByVal sender As System.Object, _
                  ByVal e As System.EventArgs) Handles btnClose.MouseLeave
        lblClose.Visible = False
    End Sub
End Class